<template>
  <div>
    <el-card class="login-form-layout">
      <el-form autoComplete="on"
               :model="loginForm"
               :rules="loginRules"
               ref="loginForm"
               label-position="left">
        <div style="text-align: center">
          <svg-icon icon-class="login-mall" style="width: 56px;height: 56px;color: #409EFF"></svg-icon>
        </div>
        <h2 class="login-title color-main">估价系统后台管理系统</h2>
        <el-form-item prop="username">
          <el-input name="username"
          type="text"
          v-model="loginForm.username"
          autoComplete="on"
          placeholder="请输入用户名">
            <span slot="prefix">
              <svg-icon icon-class="user" class="color-main"></svg-icon>
            </span>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input name="password"
          :type="pwdType"
          @keyup.enter.native="handleLogin"
          v-model="loginForm.password"
          autoComplete="on"
          placeholder="请输入密码">
            <span slot="prefix">
              <svg-icon icon-class="password" class="color-main"></svg-icon>
            </span>
            <span slot="suffix" @click="showPwd">
              <svg-icon icon-class="eye" class="color-main"></svg-icon>
            </span>
          </el-input>
        </el-form-item>
        <el-form-item style="margin-bottom: 60px">
          <el-button style="width: 100%" type="primary" :loading="loading" @click.native.prevent="handleLogin">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <img :src="login_center_bg" class="login-center-layout">
    <!-- <el-dialog
      title="特别赞助"
      :visible.sync="dialogVisible"
      width="30%">
      <span>mall项目已由CODING特别赞助，点击去支持，页面加载完后点击<span class="color-main font-medium">免费体验</span>按钮即可完成支持，谢谢！</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogCancel">残忍拒绝</el-button>
    <el-button type="primary" @click="dialogConfirm">去支持</el-button>
  </span>
    </el-dialog> -->
  </div>
</template>

<script>
  import request from "@/utils/request";
  import {isvalidUsername} from '@/utils/validate';
  import {setSupport,getSupport,SupportUrl} from '@/utils/support';
  import login_center_bg from '@/assets/images/login_center_bg.png'
  import { getToken, setToken, removeToken } from '@/utils/auth'

  export default {
    name: 'login',
    data() {
      const validateUsername = (rule, value, callback) => {
        if (!isvalidUsername(value)) {
          callback(new Error('请输入正确的用户名'))
        } else {
          callback()
        }
      };
      const validatePass = (rule, value, callback) => {
        if (value.length < 5) {
          callback(new Error('密码不能小于5位'))
        } else {
          callback()
        }
      };
      return {
        loginForm: {
          username: 'admin',
          password: 'admin',
        },
        loginRules: {
          username: [{required: true, trigger: 'blur'}],
          password: [{required: true, trigger: 'blur'}]
        },
        loading: false,
        pwdType: 'password',
        login_center_bg,
        dialogVisible:false
      }
    },
    methods: {
      showPwd() {
        if (this.pwdType === 'password') {
          this.pwdType = ''
        } else {
          this.pwdType = 'password'
        }
      },
      handleLogin() {
        console.log(this.loginForm.username)
        
        request.post("/admin/login/login",{
            username:this.loginForm.username,
            password:this.loginForm.password,
        }).then(res => {
            if (res.code == 200) {
              setToken(res.data.token)
              this.$message({
                // type: res.errno === 0 ? "success" : "warning",
                type: "success",
                message: '登录成功！'
              });
              localStorage.setItem('username', this.loginForm.username);//使用localstorage将用户名存储到user里
              this.$router.push({path: '/'})
            }
        });
        // this.$refs.loginForm.validate(valid => {
        //   if (valid) {
        //     let isSupport = getSupport();
        //     this.loading = true;
        //     this.$store.dispatch('Login', this.loginForm).then(() => {
        //       alert('2')
        //       this.$message({
        //         // type: res.errno === 0 ? "success" : "warning",
        //         type: "success",
        //         message: '登录成功！'
        //       });
        //       console.log(valid)
        //       this.loading = false;
        //       // , query: {username: this.loginForm.username}
        //       this.$router.push({path: '/'})
              
        //       localStorage.setItem('username', this.loginForm.username);//使用localstorage将用户名存储到user里
        //       localStorage.setItem('password', this.loginForm.password);//使用localstorage将用户名存储到user里
        //     }).catch(() => {
        //       this.loading = false
        //     })
        //   } else {
        //     this.$message({
        //         type: "warning",
        //         message: "验证失败!"
        //     });
        //     console.log('参数验证不合法！');
        //     return false
        //   }
        // })
      },
      dialogConfirm(){
        this.dialogVisible =false;
        setSupport(true);
        window.location.href=SupportUrl;
      },
      dialogCancel(){
        this.dialogVisible = false;
        setSupport(false);
      }
    }
  }
</script>

<style scoped>
  .login-form-layout {
    position: absolute;
    left: 0;
    right: 0;
    width: 360px;
    margin: 140px auto;
    border-top: 10px solid #409EFF;
  }

  .login-title {
    text-align: center;
  }

  .login-center-layout {
    background: #409EFF;
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    margin-top: 200px;
  }
</style>
